#!/usr/bin/env python

import sys
import pyxmppxmpp as gxmpp
import commandfactory
import gootalk
import userman
import logging
import traceback

OK=0
ERROR=-1
COMMAND_MARK='#'

jid = None

def onMessage(sender, body):
    logging.info(u'onMessage %s %s' % (sender, body))

    try:
        logging.info(u'onMessage DealCommand %s' % (body))

        dealRet = gootalk.ERROR
        if (body[:1] == COMMAND_MARK and body.count >= 2):
            if (body[1:1] != COMMAND_MARK):
                dealRet = commandfactory.dealCommand(sender, body[1:])
            else:
                body = body[1:]

        logging.info(u'onMessage AddUser %s' % (sender))
        u = userman.addUser(sender)

        logging.info(u'onMessage SendToOther %s' % (body))
        if (dealRet != gootalk.OK):
            gxmpp.sendMessageToOther(u.jid, u'[' + u.nick + u'] ' + body)
            
        logging.info(u'onMessaged %s %s' % (sender, body))
    except:
        logging.info(traceback.print_exc())
